﻿
using Microsoft.Office.Interop.Excel;
//using Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


//using Excel;


namespace test.word.excel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Data.DataTable dt1 = new System.Data.DataTable();
            dt1.Columns.Add("数");
            dt1.Columns.Add("平方");

            for (int i = 0; i < 10; i++)
            {
                DataRow dr1 = dt1.NewRow();
                dr1[0] = i.ToString();
                dr1[1] = (i * i).ToString();
                dt1.Rows.Add(dr1);

            }

            this.dataGridView1.DataSource = dt1;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Microsoft.Office.Interop.Excel.ApplicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
            ApplicationClass ExcelApp  = new ApplicationClass();
            ExcelApp.Caption = "应用程序调用 Microsoft Excel";
            ExcelApp.Visible = true;


            //Microsoft.Office.Interop.Excel.Workbook newWorkbook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            string workbookPath = Environment.CurrentDirectory + "\\1.xls";  // Add your own path here
            //var f = new FileInfo("1.xls");
            //f.Create();
            
            

            //Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Open(workbookPath, 0,
            //   false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
            //   false, 0, true, false, false);
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

            Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A1");
            excelCell.Value2 = "Hello world\nhi";
            



            //WorkbookClass newWorkbook = new WorkbookClass();
            //var workbook=ExcelApp.Workbooks.Add();


            //ExcelApp.SaveWorkspace( "Demo1.xls" ); 

 



            //Class1111.main1();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            ApplicationClass ExcelApp = new ApplicationClass();
            ExcelApp.Caption = "应用程序调用 Microsoft Excel";
            //ExcelApp.Visible = true;
            ExcelApp.Visible = false;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);
            Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("a1");
            excelCell.Value2 = "Hello world";
            ExcelApp.SaveWorkspace("Demo1.xls"); 
        }
    }



    class Class1111
    {

        //public static void main1()
        //{
        //    Excel.Application excelApp = new Excel.ApplicationClass();  // Creates a new Excel Application
        //    excelApp.Visible = true;  // Makes Excel visible to the user.

        //    // The following line adds a new workbook
        //    Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

        //    // The following code opens an existing workbook
        //    string workbookPath = Environment.CurrentDirectory + "\\1.xls";  // Add your own path here
        //    Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
        //        false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
        //        false, 0, true, false, false);

        //    // The following gets the Worksheets collection
        //    Excel.Sheets excelSheets = excelWorkbook.Worksheets;

        //    // The following gets Sheet1 for editing
        //    string currentSheet = "Sheet1";
        //    Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

        //    // The following gets cell A1 for editing
        //    Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");

        //    // The following sets cell A1's value to "Hi There"
        //    excelCell.Value2 = "Hi There";

        //    object missingValue = Type.Missing;
        //    Excel.Range rng = excelWorksheet.get_Range("A1",
        //        missingValue);
        //    Excel.Style style;

        //    try
        //    {
        //        style = excelWorkbook.Styles["NewStyle"];
        //    }
        //    // Style doesn't exist yet.
        //    catch
        //    {
        //        style = excelWorkbook.Styles.Add("NewStyle", missingValue);
        //        style.Font.Name = "Verdana";
        //        style.Font.Size = 12;
        //        style.Font.Color = 255;
        //        style.Interior.Color = (200 << 16) | (200 << 8) | 200;
        //        style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
        //    }

        //    rng.Value2 = "'Style Test";
        //    rng.Style = "NewStyle";
        //    rng.Columns.AutoFit();
        //}
    }

}
